DESTDIR := 
PREFIX := /usr/local

all: clean gen-packed-go format-code
	go mod tidy
	# strip symbol and remove dwarf debug info
	GOPROXY=goproxy.cn go build -o bin/ttyping -ldflags "-s -w" src/cmd/*.go

format-code:
	gofmt -w src/**/*.go

upx-most-zip:
	upx -9 bin/ttyping

run: all
	./bin/ttyping

install:
	install -d $(DESTDIR)$(PREFIX)/bin
	install -d $(DESTDIR)$(PREFIX)/share/man/man1
	install -m755 bin/ttyping $(DESTDIR)$(PREFIX)/bin
	install -m644 docs/mandoc/ttyping.1.gz $(DESTDIR)$(PREFIX)/share/man/man1

uninstall:
	rm -rf $(DESTDIR)$(PREFIX)/bin/ttyping
	rm -rf $(DESTDIR)$(PREFIX)/share/man/man1/ttyping.1.gz

gen-packed-go:
	cp helper-scripts/pack-to-src-packed-go.sh .
	chmod +x pack-to-src-packed-go.sh
	./pack-to-src-packed-go.sh themes/ db_words/ db_quotes/ > src/go_ttyping/packed.go
	rm -rf pack-to-src-packed-go.sh

gen-mandoc: download-pandoc
	- pandoc -s -t man -o - docs/mandoc/ttyping.md | gzip > docs/mandoc/ttyping.1.gz
	./pandoc-*/bin/pandoc -s -t man -o - docs/mandoc/ttyping.md | gzip > docs/mandoc/ttyping.1.gz


download-pandoc:
	# mirrored download, consider remove mirrored prefix
	wget -O pandoc.tar.gz https://ghproxy.cn/https://github.com/jgm/pandoc/releases/download/3.5/pandoc-3.5-linux-amd64.tar.gz
	# extract tar.gz
	tar xvfh pandoc.tar.gz

gen-themes:
	python3 helper-scripts/themegen.py

gen-all-first: gen-packed-go gen-mandoc gen-themes

release:
	GOOS=darwin GOARCH=amd64 go build -o bin/tt-osx src/cmd/*.go
	GOOS=windows GOARCH=amd64 go build -o bin/tt.exe src/cmd/*.go
	GOOS=linux GOARCH=amd64 go build -o bin/tt-linux src/cmd/*.go
	GOOS=linux GOARCH=arm go build -o bin/tt-linux_arm src/cmd/*.go
	GOOS=linux GOARCH=arm64 go build -o bin/tt-linux_arm64 src/cmd/*.go

dist-with-goreleaser:
	goreleaser --snapshot --clean

clean:
	rm -rf bin dist pandoc-* docs/mandoc/*1.gz *.tar.gz